import { App, Provide, Inject, Controller, Get, Post, Param } from '@midwayjs/decorator'
import { Application, Context } from 'egg'

import { BaseController } from './base'
import { UploadService } from '../service/upload'

@Provide()
@Controller('/api/upload')
export class UploadController extends BaseController {

  @App()
  app: Application

  @Inject()
  ctx: Context

  @Inject()
  uploadService: UploadService

  @Post('/')
  async upload() {
    const stream = await this.ctx.getFileStream()

    return this.ok(
      await this.uploadService.upload(stream)
    )
  }

  @Post('/:id/delete')
  async remove(@Param() id: number) {
    return this.ok(
      await this.uploadService.remove(id)
    )
  }

  @Get('/:id')
  async download(@Param() id: number) {
    return await this.uploadService.download(id)
  }
}
